﻿var _modalEventArgsArray;
var _modalEventQueue; 

function ModalEventArgs(clientId, uniqueId, containerDiv, contentDiv, progressDiv, hideDropDowns, valign, halign, voffset, hoffset) {
    this.ClientId = clientId;
    this.UniqueId = uniqueId;
    this.ContainerDiv = containerDiv;
    this.ContentDiv = contentDiv;
    this.ProgressDiv = progressDiv;
    this.HideDropDowns = hideDropDowns;
    this.VAlign = valign;
    this.HAlign = halign;
    this.VOffset = voffset;
    this.HOffset = hoffset;
}
function AddModalEventArgs(clientId, uniqueId, containerDiv, contentDiv, progressDiv, hideDropDowns, valign, halign, voffset, hoffset) {
    VerifyEventArray();
    if (!FindModalEventArgs(clientId)) {
        _modalEventArgsArray[_modalEventArgsArray.length] = new ModalEventArgs(clientId, uniqueId, containerDiv, contentDiv, progressDiv, hideDropDowns, valign, halign, voffset, hoffset);
    }    
}
function FindModalEventArgs(clientId) {
    VerifyEventArray();
    for (var i = 0; i < _modalEventArgsArray.length; i++) {
        if (_modalEventArgsArray[i].ClientId == clientId) {
            return _modalEventArgsArray[i];
        }
    }
    return null;
}
function VerifyEventArray() {
    if (_modalEventArgsArray == null) {
        _modalEventArgsArray = new Array();
    }
}
function VerifyEventQueue() {
    if (_modalEventQueue == null) {
        _modalEventQueue = new Array();
    }
}
function Modalize(elem) {
    VerifyEventArray();
    var eventArgs = FindModalEventArgs(elem.id);
    if (eventArgs != null) {
        var containerDiv = $get(eventArgs.ContainerDiv);
        var overlayDiv = $get(eventArgs.ContainerDiv + "_OverlayDiv");
        var messageDiv = $get(eventArgs.ContainerDiv + "_MessageDiv");
        var contentDiv = $get(eventArgs.ContentDiv);
        if (contentDiv && containerDiv) {
            containerDiv.style.display = '';
            overlayDiv.style.display = '';
            messageDiv.style.display = '';

            if (eventArgs.HideDropDowns == true) {
                var inputElems = _contentDiv.getElementsByTagName('select');
                for (var i = 0; i < inputElems.length; i++) {
                    if (inputElems[i] != null) {
                        inputElems[i].style.display = 'none';
                    }
                }
            }

            var contentBounds = Sys.UI.DomElement.getBounds(contentDiv);
            var messageBounds = Sys.UI.DomElement.getBounds(messageDiv);
            var containerBounds = Sys.UI.DomElement.getBounds(containerDiv);

            var xRatio;
            var yRatio;

            if (eventArgs.VAlign == "Top") {
                yRatio = 0;
            }
            else if (eventArgs.VAlign == "Bottom") {
                yRatio = 1;
            }
            else {
                yRatio = .5;
            }

            if (eventArgs.HAlign == "Left") {
                xRatio = 0;
            }
            else if (eventArgs.HAlign == "Right") {
                xRatio = 1;
            }
            else {
                xRatio = .5;
            }

            var messageX = contentBounds.x + Math.round(contentBounds.width * xRatio) - Math.round(messageBounds.width * xRatio);
            var messageY = contentBounds.y + Math.round(contentBounds.height * yRatio) - Math.round(messageBounds.height * yRatio);
            messageY = messageY + eventArgs.VOffset;
            messageX = messageX + eventArgs.HOffset;

            overlayDiv.style.width = contentBounds.width + 'px';
            overlayDiv.style.height = contentBounds.height + 'px';

            Sys.UI.DomElement.setLocation(overlayDiv, contentBounds.x - containerBounds.x, contentBounds.y - containerBounds.y);
            Sys.UI.DomElement.setLocation(messageDiv, messageX - containerBounds.x, messageY - containerBounds.y);

            if (eventArgs.ProgressDiv != null) {
                var progressDiv = $get(eventArgs.ProgressDiv);
                if (eventArgs.ProgressDiv != null) {
                    progressDiv.style.visibility = 'hidden';
                    progressDiv.style.display = 'none';
                }
            }
        }
    }
}
function InitModalLoading() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_initializeRequest(InitModalLoadRequest);
        prm.add_endRequest(EndModalLoadRequest);
    }
}
function EndModalLoadRequest(sender, args) {
    VerifyEventArray();
    for (var i = 0; i < _modalEventArgsArray.length; i++) {
        var containerDiv = $get(_modalEventArgsArray[i].ContainerDiv);
        if (containerDiv) {
            var overlayDiv = $get(_modalEventArgsArray[i].ContainerDiv + "_OverlayDiv");
            var messageDiv = $get(_modalEventArgsArray[i].ContainerDiv + "_MessageDiv");
            containerDiv.style.display = 'none';
            overlayDiv.style.display = 'none';
            messageDiv.style.display = 'none';
            if (_modalEventArgsArray[i].ProgressDiv != null) {
                var progressDiv = $get(eventArgs.ProgressDiv);
                if (eventArgs.ProgressDiv != null) {
                    $get(_modalEventArgsArray[i].ProgressDiv).style.visibility = '';
                }
            } 
        }
    }
    VerifyEventQueue();
    if (_modalEventQueue.length > 0) {
        __doPostBack(_modalEventQueue[0], '');
        Array.removeAt(_modalEventQueue, 0);
    }

}
function InitModalLoadRequest(sender, args) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    var elem = args.get_postBackElement();
    Modalize(elem);
    if (prm.get_isInAsyncPostBack()) {
        VerifyEventArray();
        var eventArgs = FindModalEventArgs(elem.id);
        if (eventArgs != null) {
            VerifyEventQueue();
            Array.add(_modalEventQueue, eventArgs.UniqueId);
        }
        args.set_cancel(true);
    }
}